<?php
/* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
 * See the file COPYRIGHT.html for more details.
 */
 
  require_once("../shared/common.php");
  $tab = "circulation";
  $restrictToMbrAuth = TRUE;
  $nav = "newconfirm";
  $restrictInDemo = true;
  require_once("../shared/logincheck.php");

  require_once("../classes/Member.php");
  require_once("../classes/MemberQuery.php");
  require_once("../classes/DmQuery.php");
  require_once("../functions/errorFuncs.php");
  require_once("../classes/Localize.php");
  $loc = new Localize(OBIB_LOCALE,$tab);

  #****************************************************************************
  #*  Checking for post vars.  Go back to form if none found.
  #****************************************************************************
  if (count($_POST) == 0) {
    header("Location: ../circ/mbr_new_form.php");
    exit();
  }
  $info = $_POST["info"];

  #****************************************************************************
  #*  Validate data
  #****************************************************************************
  $mbr = new Member();
  $mbr->setBarcodeNmbr($_POST["barcodeNmbr"]);
  $_POST["barcodeNmbr"] = $mbr->getBarcodeNmbr();
  $mbr->setLastChangeUserid($_SESSION["userid"]);
  $mbr->setLastName($_POST["lastName"]);
  $_POST["lastName"] = $mbr->getLastName();
  $mbr->setFirstName($_POST["firstName"]);
  $_POST["firstName"] = $mbr->getFirstName();
  $mbr->setAddress($_POST["address"]);
  $_POST["address"] = $mbr->getAddress();
  $mbr->setHomePhone($_POST["homePhone"]);
  $_POST["homePhone"] = $mbr->getHomePhone();
  $mbr->setHomePhonePrefix($_POST["homePhonePrefix"]);
  $_POST["homePhonePrefix"] = $mbr->getHomePhonePrefix();  
  $mbr->setWorkPhone($_POST["workPhone"]);
  $_POST["workPhone"] = $mbr->getWorkPhone();
  $mbr->setWorkPhonePrefix($_POST["workPhonePrefix"]);
  $_POST["workPhonePrefix"] = $mbr->getWorkPhonePrefix();  
  $mbr->setEmail($_POST["email"]);
  $_POST["email"] = $mbr->getEmail();
  
  $mbr->setCuil($_POST["cuil"]);
  $_POST["cuil"] = $mbr->getCuil();
  $mbr->setCityId($_POST["cityid"]);
  $_POST["cityid"] = $mbr->getCityId();
  $mbr->setOperativosOld($_POST["operativos_old"]);
  $_POST["operativos_old"] = $mbr->getOperativosOld();
  $mbr->setObservaciones($_POST["observaciones"]);
  $_POST["observaciones"] = $mbr->getObservaciones();
  $mbr->setBank($_POST["bank"]);
  $_POST["bank"] = $mbr->getBank();
  $mbr->setAccountType($_POST["account_type"]);
  $_POST["account_type"] = $mbr->getAccountType();
  $mbr->setAccountNmbr($_POST["account_nmbr"]);
  $_POST["account_nmbr"] = $mbr->getAccountNumber();
  $mbr->setCbu($_POST["cbu"]);
  $_POST["cbu"] = $mbr->getCbu();
  $mbr->setAccountDesc($_POST["account_desc"]);
  $_POST["account_desc"] = $mbr->getAccountDesc();
  $mbr->setVeedorId($_POST["veedorid"]);
  $_POST["veedorid"] = $mbr->getVeedorId();
  $mbr->setClassification($_POST["classification"]);
  $_POST["classification"] = $mbr->getClassification();
  $mbr->setOperativoNmbr($_POST["operativo_nmbr"]);
  $_POST["operativo_nmbr"] = $mbr->getOperativoNmbr();      

  
  $validData = $mbr->validateData();
  if (!$validData) {
    $pageErrors["barcodeNmbr"] = $mbr->getBarcodeNmbrError();
    $pageErrors["lastName"] = $mbr->getLastNameError();
    $pageErrors["firstName"] = $mbr->getFirstNameError();
    $pageErrors["veedorid"] = $mbr->getVeedorIdError();
    $pageErrors["operativo_nmbrError"] = $mbr->getOperativoNmbrError();
    $pageErrors["cuil"] = $mbr->getCuilError();
    $pageErrors["cbu"] = $mbr->getCbuError();
    $_SESSION["postVars"] = $_POST;
    $_SESSION["pageErrors"] = $pageErrors;
    header("Location: ../circ/mbr_new_form.php?info=".$info);
    exit();
  }

  #**************************************************************************
  #*  Check for duplicate barcode number
  #**************************************************************************
  $mbrQ = new MemberQuery();
  $mbrQ->connect();
  $dupBarcode = $mbrQ->DupBarcode($mbr->getBarcodeNmbr(),$mbr->getMbrid());
  if ($dupBarcode) {
    $pageErrors["barcodeNmbr"] = $loc->getText("mbrDupBarcode",array("barcode"=>$mbr->getBarcodeNmbr()));
    $_SESSION["postVars"] = $_POST;
    $_SESSION["pageErrors"] = $pageErrors;
    header("Location: ../circ/mbr_new_form.php");
    exit();
  }
  
  #**************************************************************************
  #*  Check for duplicate Operative Number
  #**************************************************************************
  $mbrQ = new MemberQuery();
  $mbrQ->connect();
  if($mbr->getOperativoNmbr() != null && $mbr->getOperativoNmbr() > 0){
  	$dupOperativoNmbr = $mbrQ->DupOperativeNumber($mbr->getOperativoNmbr(),$mbr->getMbrid());
  	if ($dupOperativoNmbr) {
  		$pageErrors["operativo_nmbr"] = $loc->getText("mbrDupOperativoNmbr",array("operativo_nmbr"=>$mbr->getOperativoNmbr()));
  		$_SESSION["postVars"] = $_POST;
  		$_SESSION["pageErrors"] = $pageErrors;
  		header("Location: ../circ/mbr_edit_form.php");
  		exit();
  	}  	
  }  

  #**************************************************************************
  #*  Insert new library member
  #**************************************************************************
  $mbrid = $mbrQ->insert($mbr);
  $mbrQ->close();

  #**************************************************************************
  #*  Destroy form values and errors
  #**************************************************************************
  unset($_SESSION["postVars"]);
  unset($_SESSION["pageErrors"]);

  $msg = $loc->getText("mbrNewSuccess");
  header("Location: ../circ/mbr_view.php?mbrid=".U($mbrid)."&reset=Y&msg=".U($msg));
  exit();
?>
